package com.abl.service.system.impl;


import com.abl.common.cache.CacheKey;
import com.abl.common.cache.CacheService;
import com.abl.common.mybatis.dao.Dao;
import com.abl.common.mybatis.model.Dnt;
import com.abl.common.mybatis.strengthen.Wrapper;
import com.abl.domain.model.Param;
import com.abl.common.util.StringKit;
import com.abl.service.system.SysDictService;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 字典表实现
 */
@Slf4j
@Service
public class SysDictServiceImpl implements SysDictService {
    @Autowired
    private Dao dao;
    @Autowired
    private CacheService cache;

    //新增
    @Override
    public long add(Param<JSONObject> param) {
        param.remove("id");
        Dnt dnt = Dnt.createInsert(param);
        long sys_dict = dao.insert("sys_dict", dnt);

        //更新缓存
        refreshCache(null, null);

        return sys_dict;
    }

    //删除
    @Override
    public long del(Param<JSONObject> param) {
        String key = param.getString("type");
        String code = param.getString("code");

        Wrapper<Object> uw = new Wrapper<>();
        uw.eq("d_s", "0");
        uw.eq("type", key);
        uw.eq("code", code);
        uw.set("d_s", "0");
        long sys_sys_dict = dao.update("sys_sys_dict", uw);

        //更新缓存
        refreshCache(null, null);

        return sys_sys_dict;
    }

    //修改
    @Override
    public long edit(Param<JSONObject> param) {
        String key = param.getString("type");
        String code = param.getString("code");
        String value = param.getString("value");

        Wrapper<Object> uw = new Wrapper<>();
        uw.eq("d_s", "0");
        uw.eq("type", key);
        uw.eq("code", code);

        Dnt dnt = Dnt.createUpdate();
        dnt.set("value", value);
        long sys_dict = dao.update("sys_dict", dnt, uw);

        //更新缓存
        refreshCache(null, null);

        return sys_dict;

    }

    //查询详情
    @Override
    public Dnt info(Param<JSONObject> param) {
        Wrapper<Object> qw = new Wrapper<>();
        qw.select(" id ");
        qw.eq("d_s", "0");
        qw.eq("type", param.get("type"));
        qw.eq("code", param.get("code"));
        return dao.selectOne("sys_dict", qw);
    }

    //查询集合
    @Override
    public List<Dnt> list(Param<JSONObject> param) {
        Wrapper<Object> qw = new Wrapper<>();
        qw.eq("id", param.getOrDefault("id", 0));
        qw.eq(StringKit.isNotBlank(param.getString("id")), "id", param.getString("id"));

        if (param.isPage()) {
            return dao.page("sys_dict", qw, param.pageCurrent(), param.pageSize());
        } else {
            return dao.list("sys_dict", qw);
        }

    }

    //更新缓存
    private void refreshCache(String type, String code) {
        cache.delete(CacheKey.DICT_ALL.key());
        if (type != null && code != null) {
            cache.delete("dict_value" + "_" + type + "_" + code);
        }
    }

}
